document.write("<script src='js/httpUtil.js'></script>");
document.write("<script src='js/common.js'></script>");

$(function(){
    menuTreeList()
})

var layout = [
    { name: '菜单描述', treeNodes: true, headerClass: 'value_col', colClass: 'value_col', style: '' },
    {
        name: '操作',
        headerClass: 'value_col',
        colClass: 'value_col',
        style: 'width: 50%',
        render: function(row) {
            var resource = JSON.parse(row);
            var editBtn = "<button class=\"btn btn-sm btn-primary m-b-5 m-t-5\" onclick='openEditMenuPanel(" +resource.id + ")'>编辑</button>"
            var enabledBtn = editBtn + " <button class=\"btn btn-sm btn-danger m-b-5 m-t-5\"" +
                "onclick='changeStatus(" + resource.id + ",0)'>禁用</button>";
            if (resource.enabled!=1) {
                enabledBtn = editBtn + " <button class=\"btn btn-sm btn-info m-b-5 m-t-5\" " +
                    "onclick='changeStatus(" + resource.id + ",1)'>启用</button>";
            }
            return enabledBtn; //列渲染
        }
    },
];

function menuTreeList(){
    var jsonObj = {};
    var result=httpPost(baseUrl+"/menu/menuList",jsonObj);
    if(result.flag==1){
        $("#menuList").empty()

        layui.use(['form', 'treetable', 'layer'], function() {
            var layer = layui.layer, form = layui.form, $ = layui.jquery;

            var tree = layui.treetable({
                elem: '#menuList', //传入元素选择器
                spreadable: true, //设置是否全展开，默认不展开
                nodes: formatJson(result.result),
                layout: layout,
                callback: {
                    beforeCollapse : treetableBeforeCollapse,
                    onCollapse : treetableOnCollapse,
                    beforeExpand : treetableBeforeExpand,
                    onExpand : treetableOnExpand,
                }
            });

            function treetableBeforeCollapse(){
                return true;
            };
            function treetableOnCollapse(){
            };
            function treetableBeforeExpand(){
                return true;
            };
            function treetableOnExpand(){
            };

            form.render();
        });
    }else{
        toastr.error("系统错误",result.msg);
    }
}



function formatJson(json){
    for(var i=0;i<json.length;i++){
        json[i]["id"] = json[i]["resourceId"];
        delete json[i]["resourceId"];
        json[i]["name"] = json[i]["resourceDesc"]
        delete json[i]["resourceDesc"];
        if(json[i].children!=null){
            json[i]["children"] = formatJson(json[i].children)
        }
    }
    return json;
}


function changeStatus(id, enabled){
    var jsonObj = {"resourceId":id};
    var result = httpPost(baseUrl+"/menu/menuDetail",jsonObj);
    if(result.flag == 1){
        jsonObj = {
            "resourceId": id,
            "resourceName": result.result.resourceName,
            "resourceDesc": result.result.resourceDesc,
            "resourceString": result.result.resourceString,
            "enabled": enabled
        };
        result = httpPost(baseUrl+"/menu/editMenu",jsonObj);
        if(result.flag == 1){
            menuTreeList()
            console.log("状态修改成功，现在菜单状态：",enabled)
        }else{
            toastr.error("系统错误",result.msg);
        }
    }else{
        toastr.error("系统错误",result.msg);
    }
}

function openEditMenuPanel(key) {
    var resourceId = $("#resourceId");
    var resourceDesc = $("#resourceDesc");
    var resourceName = $("#resourceName");
    var resourceString = $("#resourceString");
    var jsonObj = {"resourceId":key};
    var result = httpPost(baseUrl+"/menu/menuDetail",jsonObj);
    if(result.flag == 1){
        enabledRadio("enabled", result.result.enabled)

        resourceId.val(result.result.resourceId)
        resourceDesc.val(result.result.resourceDesc)
        resourceName.val(result.result.resourceName)
        resourceString.val(result.result.resourceString)
    }else{
        toastr.error("系统错误",result.msg);
    }
    $('#editMenuModal').modal('show');
}

$("#editMenuBtn").click(function () {
    var resourceId = $("#resourceId").val();
    var resourceDesc = $("#resourceDesc").val();
    var resourceName = $("#resourceName").val();
    var resourceString = $("#resourceString").val();
    var enabled = $('[name=enabled]:checked').val();
    var jsonObj = {
        "resourceId": resourceId,
        "resourceName": resourceName,
        "resourceDesc": resourceDesc,
        "resourceString": resourceString,
        "enabled": enabled
    };
    var result=httpPost(baseUrl+"/menu/editMenu",jsonObj);
    if(result.flag == 1){
        toastr.success("编辑菜单成功","成功");
        $('#editMenuModal').modal('hide');
        menuTreeList();
    }else{
        toastr.error("系统错误",result.msg);
    }
})

$("#addChildMenuPanelBtn").click(function (){
    $("#addMenuModal :input").each(function () {
        $(this).val("");
    });
    $('#editMenuModal').modal('hide');
    $('#addMenuModal').modal('show');
    var resourceId = $("#resourceId").val();
    $("#parentId").val(resourceId)
})

$("#addMenuPanelBtn").click(function () {
    $("#addMenuModal :input").each(function () {
        $(this).val("");
    });
    $('#addMenuModal').modal('show');
    $("#parentId").val(0)
})

$("#addMenuBtn").click(function () {
    var parentId = $("#parentId").val();
    var addResourceDesc = $("#addResourceDesc").val();
    var addResourceName = $("#addResourceName").val();
    var addResourceString = $("#addResourceString").val();
    yorickAssert(addResourceDesc!=""&&addResourceName!=""&&parentId!="","请输入完整参数","参数错误")
    var jsonObj = {
        "resourceName":addResourceName,
        "resourceDesc":addResourceDesc,
        "parentId":parentId,
        "resourceString":addResourceString
    }
    var result=httpPost(baseUrl+"/menu/addMenu",jsonObj);
    if(result.flag == 1){
        toastr.success("新增菜单成功","成功");
        $('#addMenuModal').modal('hide');
        menuTreeList();
    }else{
        toastr.error("系统错误",result.msg);
    }
})

